package com.authine.cloudpivot.ext.controller;

import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.engine.api.model.application.AppFunctionModel;
import com.authine.cloudpivot.engine.api.model.application.AppPackageModel;
import com.authine.cloudpivot.engine.api.model.organization.UserModel;
import com.authine.cloudpivot.engine.api.model.permission.AdminModel;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectCreatedModel;
import com.authine.cloudpivot.ext.dto.CustAccessLog;
import com.authine.cloudpivot.ext.task.TaskDelayedManager;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.dubbo.DubboConfigService;
import com.authine.cloudpivot.web.api.view.organization.AdministratorVO;
import com.authine.cloudpivot.web.api.view.system.AdminVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.*;

import static java.util.concurrent.TimeUnit.NANOSECONDS;

/**
 * @Author hxd
 * @Date 2021/11/13 17:07
 * @Description
 **/
@Component
@Slf4j
public class CustAccessLogService {

    @Autowired
    DubboConfigService dubboConfigService;


    private final String logSchemaCode = "custAccessLog";

    public void addLog(CustAccessLog calog){
        if (calog == null || logSchemaCode.equals(calog.getSchemaCode())) {
            return;
        }

        Runnable runnable = () -> {

            String userId = calog.getUserId();

            if (userId != null) {
                UserModel user = dubboConfigService.getOrganizationFacade().getUser(userId);
                if (user != null) {
                    calog.setUsername(user.getUsername());
                    calog.setRealname(user.getName());
                }
            }

            AppFunctionModel function = dubboConfigService.getAppManagementFacade().getAppFunctionByCode(calog.getSchemaCode());

            if (function != null) {
                calog.setAppCode(function.getAppCode());
                calog.setModelName(function.getName());
                if (function.getAppCode() != null) {
                    AppPackageModel packageModel = dubboConfigService.getAppManagementFacade().getAppPackageByCode(function.getAppCode());
                    if (packageModel != null) {
                        calog.setAppName(packageModel.getName());
                    }
                }
            }


            BizObjectCreatedModel bizObject = new BizObjectCreatedModel(logSchemaCode, (Map<String, Object>) JSON.toJSON(calog),false);

            String s = dubboConfigService.getBizObjectFacade().saveBizObject(calog.getUserId(), bizObject, false);
            if (log.isDebugEnabled()) {
                log.debug("============日志添加成功:{}",s);
            }

        };
        //延迟10秒执行
        TaskDelayedManager.put(runnable,10,TimeUnit.SECONDS);

    }
}
